package com.link.base.tencent.corpwx.usermanage.controller;

import com.link.base.tencent.corpwx.addressbook.model.User;
import com.link.base.tencent.corpwx.agent.util.AgentIdUtil;
import com.link.base.tencent.corpwx.usermanage.service.CorpWxUserService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业用户-企业微信通讯录中的内部用户管理
 *
 * @author 李宗庭
 * @date 2019/02/27 16:04
 */
@Controller
@RequestMapping("/link/corpwx/user")
public class CorpWxUserController extends BasicController<User> {

    @Resource
    private CorpWxUserService userService;

    @Override
    public BasicService<User> getBasicService() throws Exception {
        return userService;
    }

    /**
     * 同步单个用户信息到企业微信通讯录：存在更新，不存在则新建
     *
     * @author 李宗庭
     * @date 2019/3/7
     */
    @RequestMapping(value = "/synCorpWxUserToWx")
    @ResponseBody
    public Map<String, Object> synCorpWxUserToWx(@JsonParam User entity, HttpSession session,
                                             HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            userService.synCorpWxUserToWx(entity, AgentIdUtil.CORPWX_ADDRESSBOOK_AGENTID);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
    * 增量更新当前账套用户信息到企业微信通讯录
     *
    * @author 李宗庭
    * @date 2019/3/18
    */
    @RequestMapping(value = "/synCorpWxUsersToWx")
    @ResponseBody
    public Map<String, Object> synCorpWxUsersToWx(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            String jobId =  userService.synCorpWxUsersToWx(AgentIdUtil.CORPWX_ADDRESSBOOK_AGENTID);
            result.put("success", true);
            result.put("data", jobId);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
    * 获取当前配置了客户联系功能的员工
    * @author Lizongting
    * @date 2020/1/16
    */
    @PostMapping(value = "/getFollowUsersByExamplePage", name = "获取当前配置了客户联系功能的员工")
    @ResponseBody
    public Map<String, Object> getFollowUsersByExamplePage(@JsonParam QueryParams qps, @JsonParam User entity,
                                                   HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            User temp = BasicModel.transformClass(entity, qps);
            temp.setFollowUserFlag("Y");
            List<User> list = userService.queryByExamplePage(temp);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(temp, list.size()));
            result.put("code", "200");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 全量同步企业微信配置了客户联系功能的客户至CRM
     * @author Lizongting
     * @date 2020/1/16
     */
    @GetMapping(value = "/synCorpWxFollowUserToCRM", name = "全量同步企业微信配置了客户联系功能的客户至CRM")
    @ResponseBody
    public Map<String, Object> synCorpWxFollowUserToCRM(HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            userService.synCorpWxFollowUserToCRM();
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
